package com.dwxt.parabolic.listener;

import com.dwxt.common.base.YesOrNo;
import com.dwxt.common.util.SmsUtil;
import com.dwxt.parabolic.base.EventCode;
import com.dwxt.parabolic.entity.*;
import com.dwxt.parabolic.event.EventUpload;
import com.dwxt.parabolic.service.*;
import com.dwxt.parabolic.utils.WxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 监听EventUpload事件
 */
@Component
public class EventUploadListener {

    @Autowired
    private UserService userService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private CameraService cameraService;

    @Autowired
    private CompanyService companyService;

    @EventListener
    public void handleAbstractEvent(EventUpload eventUpload) throws UnsupportedEncodingException {
        Event event = eventUpload.getEvent();
        System.out.println(event.getCameraId() + "==========");
        List<User> userList =
                userService.selectByCompanyId(event.getCompanyId()).stream().filter(user -> user.getIsAlarm() == YesOrNo.YES).collect(Collectors.toList());
        String msg = generateMsgAndUpdate(event);
        for (User user : userList) {
            msg = "account=ZTCQ888&password=ZTCQ888&mobile=" + user.getPhoneNumber() + "&content=" + java.net.URLEncoder.encode(msg, "utf-8");
            SmsUtil.SMS(msg);
            if (user.getIsBind() == 1 && user.getOpenId() != null && !EventCode.DEVICE_ERROR.getCode().equalsIgnoreCase(event.getEventCode())) {
                Camera camera = cameraService.selectByPrimaryKey(event.getCameraId());
                Company company = companyService.selectByPrimaryKey(event.getCompanyId());
                //公众号推送
                WxUtil.pushMsgToWx(user.getOpenId(),company,camera,event);
            }
        }
    }

    private String generateMsgAndUpdate(Event event) {
        Device device = deviceService.selectByPrimaryKey(event.getDeviceId());
        Camera camera = cameraService.selectByPrimaryKey(event.getCameraId());
        Company company = companyService.selectByPrimaryKey(event.getCompanyId());
        String postData = null;
        if (EventCode.PARABOLIC.getCode().equals(event.getEventCode())) {
            postData = company.getAddress() + ",摄像头：" + camera.getName() + "发生高空抛物";
        } else if (EventCode.DEVICE_ERROR.getCode().equals(event.getEventCode())) {
            postData = company.getAddress() + ",超脑：" + device.getName() + "发生故障，请检查！";
            device.setStatus(YesOrNo.NO);
            deviceService.updateByPrimaryKeySelective(device);
        } else if (EventCode.CAMERA_ERROR.getCode().equals(event.getEventCode())) {
            postData = company.getAddress() + ",摄像头：" + camera.getName() + "发生故障，请检查！";
            camera.setStatus(YesOrNo.NO);
            camera.setDescp("摄像头拉流失败！");
            cameraService.updateByPrimaryKeySelective(camera);
        }
        return postData;
    }
}
